package com.xuetang9.BBS.commons.convert;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.jasper.tagplugins.jstl.core.ForEach;
import org.springframework.core.convert.converter.Converter;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;

/**
 * 处理时间格式转化(日期转换器)
 * @author Administrator
 *
 */
public class DataConvert implements Converter<String, Date> {

	
	private  String[] patterns;
	
	public DataConvert(String[] patterns){
		this.patterns=patterns;
	}
	@Override
	public Date convert(String value) {
		if(value==null||value.length()==0){
			return null;
		}
		
		Date date=null;
		
			for (String pattern : patterns) {
				SimpleDateFormat   dateformat = new SimpleDateFormat(pattern);
			try {
				date=dateformat.parse(value);
				break;
			} catch (ParseException e) {			
		}
	}
			if(date==null){
				String message="日期格式部正确：日期必须是以下格式=>";
				for(String string :patterns){
					message +=string;
				}
				throw new RuntimeException(message);
			}
		return date;
		
	}

}
